package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.GoodsDao;
import com.jiantong.lawEnforcing.impl.dao.ProofOfServiceDao;
import com.jiantong.lawEnforcing.model.Goods;
import com.jiantong.lawEnforcing.model.ProofOfService;
import com.jiantong.lawEnforcing.service.GoodsService;
import com.jiantong.lawEnforcing.service.ProofOfServiceService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 送达回证
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class ProofOfServiceServiceImpl implements ProofOfServiceService {
    private final ProofOfServiceDao proofOfServiceDao;

    @Autowired
    public ProofOfServiceServiceImpl( ProofOfServiceDao proofOfServiceDao) {
        this.proofOfServiceDao = proofOfServiceDao;
    }


    /**
     * id 查找
     *
     * @param id
     * @return goods
     */
    @Override
    public Response<ProofOfService> finById(Long id) {
        try {
            ProofOfService goods = proofOfServiceDao.findById(id);
            if (goods != null) {
                return Response.ok(goods);
            } else {
                log.error("goods (id={}) is not found", id);
                return Response.fail("proofOfService.not.found");
            }
        }catch (Exception e){
            log.error("failed to find proofOfService(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("proofOfService.find.fail");
        }
    }

    /**
     * ids 查找一组符合
     *
     * @param ids
     * @return proofOfService
     */
    @Override
    public Response<List<ProofOfService>> finByIds(List<Long> ids) {
        try {
            List<ProofOfService> proofOfServicesList = proofOfServiceDao.findByIds(ids);
            if (proofOfServicesList != null) {
                return Response.ok(proofOfServicesList);
            } else {
                log.error("proofOfServicesList (id={}) is not found", ids);
                return Response.fail("proofOfServicesList.not.found");
            }
        }catch (Exception e){
            log.error("failed to find goodsList(ids={}),cause:{}",ids, Throwables.getStackTraceAsString(e));
            return Response.fail("goodsList.find.fail");
        }
    }

    /**
     * 创建新闻新单据
     *
     * @param proofOfService 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(ProofOfService proofOfService) {
        try {
            proofOfServiceDao.create(proofOfService);
            return Response.ok(proofOfService.getId());
        }catch (Exception e) {
            log.error("failed.to.create.proofOfService:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.proofOfService.failed");
        }
    }

    /**
     * 更新新闻的单据
     *
     * @param proofOfService 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(ProofOfService proofOfService) {
        try{
            return Response.ok(proofOfServiceDao.update(proofOfService));
        }catch (Exception e){
            log.error("failed.to.update.proofOfService:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("update.proofOfService.failed");
        }
    }
}
